--    Author    : MAYANTING
--    Name      : ADM.RPT_SUST_DLMG_PAST_INDU_LOAB.HQL
--    Functions : 表26：贷款余额分行业统计表
--    Purpose   :
--    Revisions or Comments
--    VER        DATE        AUTHOR           DESCRIPTION
--   ---------  ----------  ---------------  ------------------------------------
--    1.0        2018-06-11  MAYANTING           1.CREATE THE PROCEDURE
--
INSERT OVERWRITE TABLE ADM.RPT_SUST_DLMG_PAST_INDU_LOAB PARTITION (DATA_DATE = '#V_DATA_DATE#')
SELECT  AREA.AREA_CODE_4              AS FIN_ORG_DIST
       ,T.CCY                         AS CCY
       ,T.CLIENT_INDUSTRY             AS CLIENT_INDUSTRY
       ,T.INDUSTRY_DESC               AS INDUSTRY_DESC
       ,SUM(T.ACTUAL_BAL)/100000000   AS ACTUAL_BAL      -- 贷款余额
       ,SUM(T.BAL_LM    )/100000000   AS BAL_LM          -- 余额上期末
       ,SUM(T.BAL_YF    )/100000000   AS BAL_YF          -- 余额年初 0101
       ,SUM(T.BAL_GROW  )/100000000   AS BAL_GROW        -- 同比增长
       ,SUM(T.WSUM_BAL  )/100000000   AS WSUM_BAL        -- 余额加权值
       ,SUM(T.WBAL_LM   )/100000000   AS WBAL_LM         -- 余额加权值上期末
       ,SUM(T.WBAL_YF   )/100000000   AS WBAL_YF         -- 余额加权值年初
       ,SUM(T.WBAL_LC   )/100000000   AS WBAL_LC         -- 余额加权值去年同期
FROM (
    -- 贷款主体行业类别 一级
    SELECT
     YE.FIN_ORG_NO                                                AS FIN_ORG_NO
    ,YE.CCY                                                       AS CCY
    ,INDU.INDUSTRY_CODE_1                                         AS CLIENT_INDUSTRY  -- 贷款主体行业类别
    ,INDU.INDUSTRY_DSCR_1                                         AS INDUSTRY_DESC    -- 贷款主体行业类别描述
    ,COALESCE(YE.ACTUAL_BAL,0)                                    AS ACTUAL_BAL       -- 贷款余额
    ,COALESCE(YE.BAL_LM,0)                                        AS BAL_LM           -- 余额上期末
    ,COALESCE(YE.BAL_YF,0)                                        AS BAL_YF           -- 余额年初 0101
    ,COALESCE(YE.ACTUAL_BAL,0) - COALESCE(YE.BAL_LC,0)            AS BAL_GROW         -- 同比增长
    ,COALESCE(YE.WSUM_BAL,0)                                      AS WSUM_BAL         -- 余额加权值
    ,COALESCE(YE.WBAL_LM,0)                                       AS WBAL_LM          -- 余额加权值上期末
    ,COALESCE(YE.WBAL_YF,0)                                       AS WBAL_YF          -- 余额加权值年初
    ,COALESCE(YE.WBAL_LC,0)                                       AS WBAL_LC          -- 余额加权值去年同期
    FROM (SELECT * FROM EDW.DS_LOAB_SUM WHERE DATA_DATE='#V_DATA_DATE#') YE
    -- 关联经济成分类别维度表 行业类别只显示一级
    LEFT JOIN DIMENSION.DIM_INDUSTRY INDU ON YE.CLIENT_INDUSTRY=INDU.INDUSTRY_CODE_1 AND '#V_DATA_DATE#' BETWEEN INDU.START_DATE AND INDU.END_DATE

    UNION ALL
    -- 合计
    SELECT
     YE.FIN_ORG_NO                                                AS FIN_ORG_NO
    ,YE.CCY                                                       AS CCY
    ,'HJ'                                                         AS CLIENT_INDUSTRY   -- 贷款主体行业类别
    ,'合计'                                                       AS INDUSTRY_DESC     -- 贷款主体行业类别描述
    ,COALESCE(YE.ACTUAL_BAL,0)                                    AS ACTUAL_BAL        -- 贷款余额
    ,COALESCE(YE.BAL_LM,0)                                        AS BAL_LM            -- 余额上期末
    ,COALESCE(YE.BAL_YF,0)                                        AS BAL_YF            -- 余额年初 0101
    ,COALESCE(YE.ACTUAL_BAL,0) - COALESCE(YE.BAL_LC,0)            AS BAL_GROW          -- 同比增长
    ,COALESCE(YE.WSUM_BAL,0)                                      AS WSUM_BAL          -- 余额加权值
    ,COALESCE(YE.WBAL_LM,0)                                       AS WBAL_LM           -- 余额加权值上期末
    ,COALESCE(YE.WBAL_YF,0)                                       AS WBAL_YF           -- 余额加权值年初
    ,COALESCE(YE.WBAL_LC,0)                                       AS WBAL_LC           -- 余额加权值去年同期
    FROM (SELECT * FROM EDW.DS_LOAB_SUM WHERE DATA_DATE='#V_DATA_DATE#') YE
  )T
 -- 与地区表最细级关联
 LEFT JOIN (SELECT * FROM DIMENSION.T_ORG_AREA_RELATION WHERE AREA_LEVEL='3') REAL ON T.FIN_ORG_NO=REAL.ORG_ID AND '#V_DATA_DATE#' BETWEEN REAL.START_DATE AND REAL.END_DATE
 LEFT JOIN DIMENSION.T_AREA_CODE AREA ON REAL.AREA_CODE=AREA.AREA_CODE_4 AND '#V_DATA_DATE#' BETWEEN AREA.START_DATE AND AREA.END_DATE
 GROUP BY  AREA.AREA_CODE_4
          ,T.CCY
          ,T.CLIENT_INDUSTRY
          ,T.INDUSTRY_DESC
;